package com.ruoyi.web.ydz.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.ydz.domain.DoctorDisease;
import com.ruoyi.web.ydz.domain.HospitalDisease;
import com.ruoyi.web.ydz.mapper.HospitalDiseaseMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.web.ydz.mapper.HospitalMapper;
import com.ruoyi.web.ydz.domain.Hospital;
import com.ruoyi.web.ydz.service.IHospitalService;

import javax.annotation.Resource;

/**
 * 医院Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-11
 */
@Service
public class HospitalServiceImpl implements IHospitalService 
{
    @Resource
    private HospitalMapper hospitalMapper;

    @Resource
    private HospitalDiseaseMapper hospitalDiseaseMapper;

    /**
     * 查询医院
     * 
     * @param id 医院主键
     * @return 医院
     */
    @Override
    public Hospital selectHospitalById(Long id)
    {
        Hospital hospital = hospitalMapper.selectHospitalById(id);
        if (!Objects.isNull(hospital)){
            // 获取医院相关疾病
            HospitalDisease hospitalDisease = new HospitalDisease();
            hospitalDisease.setHospitalId(id);
            List<HospitalDisease> hospitalDiseases = hospitalDiseaseMapper.selectHospitalDiseaseList(hospitalDisease);
            hospital.setJapanDiseaseList(hospitalDiseases);
        }
        return hospital;
    }

    /**
     * 查询医院列表
     * 
     * @param hospital 医院
     * @return 医院
     */
    @Override
    public List<Hospital> selectHospitalList(Hospital hospital)
    {
        return hospitalMapper.selectHospitalList(hospital);
    }

    /**
     * 新增医院
     * 
     * @param hospital 医院
     * @return 结果
     */
    @Override
    public int insertHospital(Hospital hospital)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        hospital.setCreateTime(DateUtils.getNowDate());
        hospital.setCreateBy(user.getUserName());
        hospital.setCreateName(user.getNickName());
        hospital.setUpdateBy(user.getNickName());
        hospital.setUpdateName(user.getUserName());
        hospital.setUpdateTime(DateUtils.getNowDate());

        int i = hospitalMapper.insertHospital(hospital);

        if (!Objects.isNull(hospital.getJapanDiseaseList())){
            hospital.getJapanDiseaseList().forEach(bean -> {
                bean.setCreateTime(DateUtils.getNowDate());
                bean.setCreateBy(user.getUserName());
                bean.setCreateName(user.getNickName());
                bean.setHospitalId(hospital.getId());

                hospitalDiseaseMapper.insertHospitalDisease(bean);
            });
        }

        return i;
    }

    /**
     * 修改医院
     * 
     * @param hospital 医院
     * @return 结果
     */
    @Override
    public int updateHospital(Hospital hospital)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        hospital.setUpdateBy(user.getNickName());
        hospital.setUpdateName(user.getUserName());
        hospital.setUpdateTime(DateUtils.getNowDate());

        if (!Objects.isNull(hospital.getJapanDiseaseList())){
            hospital.getJapanDiseaseList().forEach(bean -> {
                if (Objects.isNull(bean.getId())){
                    bean.setCreateTime(DateUtils.getNowDate());
                    bean.setCreateBy(user.getUserName());
                    bean.setCreateName(user.getNickName());
                    bean.setHospitalId(hospital.getId());

                    hospitalDiseaseMapper.insertHospitalDisease(bean);
                }else{
                    hospitalDiseaseMapper.updateHospitalDisease(bean);
                }
            });
        }

        return hospitalMapper.updateHospital(hospital);
    }

    /**
     * 批量删除医院
     * 
     * @param ids 需要删除的医院主键
     * @return 结果
     */
    @Override
    public int deleteHospitalByIds(Long[] ids)
    {
        return hospitalMapper.deleteHospitalByIds(ids);
    }

    /**
     * 删除医院信息
     * 
     * @param id 医院主键
     * @return 结果
     */
    @Override
    public int deleteHospitalById(Long id)
    {
        return hospitalMapper.deleteHospitalById(id);
    }
}
